package com.jbp.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.jbp.model.Gift;
import com.jbp.model.GiftLot;
import com.jbp.service.GiftLotService;
import com.jbp.service.GiftService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.ListOperations;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.core.ValueOperations;
import org.springframework.stereotype.Service;

import java.util.*;

/**
 * redis奖池服务
 * */
@Service
public class LuckPoolRedisService {
    @Autowired
    private RedisTemplate redisTemplate;

    //普通奖池前缀
    public static final String poolPre = "pool:";

    //奖池礼物前缀
    public static final String giftPre = "pool:gift:";

    @Autowired
    private GiftService giftService;

    @Autowired
    private GiftLotService giftLotService;
    /**
     * 查看普通奖池数量
     * @param prizeDeployId 奖池id
     * */
    public long count(Integer prizeDeployId) {
        ListOperations<String, Integer> list = redisTemplate.opsForList();
        Long size = list.size(poolPre+"normal:" + prizeDeployId.intValue());
        if (size == null) {
            size = 0l;
        }
        return size;
    }

    /**
     * 生成奖池
     * @param giftIds 奖品列表
     * @param prizeDeployId 奖池id
     */
    public void generate(List<Integer> giftIds, Integer prizeDeployId, String category) {
        if(giftIds!=null && giftIds.size()>0) {
            ListOperations<String, Integer> list = redisTemplate.opsForList();
            list.rightPushAll(poolPre + category + ":" + prizeDeployId.intValue(), giftIds);
        }
    }

    /**
     * 存放奖池礼物数量，便于统计
     * @param giftLots
     */
    public void addGiftCount(List<GiftLot> giftLots) {

        giftLots.forEach(giftLot->{
            ValueOperations valueOperations = redisTemplate.opsForValue();
            String giftKey = giftPre+giftLot.getCategory()+":"+giftLot.getPoolId()+":"+giftLot.getGiftId();
            Integer count = (Integer) valueOperations.get(giftKey);
            if (count==null) {
                count = 0;
            }
            valueOperations.set(giftKey, count + giftLot.getGiftNum());
        });
    }

    /**
     * 抽奖
     * @param size 奖品数量
     * @param prizeDeployId 奖池id
     * @param category 奖池分类
     * @param limit 是否限制礼物大小  投入量不足5000鱼饼的 不能出5200以上的礼物
     */
    public List<Gift> getPoolList(Integer prizeDeployId, int size, String category, boolean limit) {
        ArrayList<Gift> gifts = new ArrayList<>();
        ListOperations<String, Integer> list = redisTemplate.opsForList();
        ValueOperations valueOperations = redisTemplate.opsForValue();
        List<Integer> replaceIds = new ArrayList<>();
        for (int i = 0; i < size; i++) {
            Integer giftId = list.leftPop(poolPre+ category+":" + prizeDeployId.intValue());
            if (giftId!=null) {
                Gift gift = giftService.getById(giftId);
                if (prizeDeployId!=-1 && limit && gift.getCoin()>5200) {
                    //取奖池配置的第一个礼物
                    GiftLot giftLot = giftLotService.getOne(new QueryWrapper<GiftLot>()
                            .eq("pool_id", prizeDeployId)
                            .eq("state", "putong")
                            .eq("category", "normal")
                            .orderByAsc("sort")
                            .last("limit 1")
                    );
                    if (giftLot!=null) {
                        System.out.println("原礼物："+gift.getName());
                        gift = giftService.getById(giftLot.getGiftId());
                        System.out.println("替换后："+gift.getName());
                        replaceIds.add(giftId);
                    }
                }
                gifts.add(gift);
            }

            //更新奖池礼物数量
            String giftKey = giftPre+category+":"+prizeDeployId+":"+giftId;
            Integer count = (Integer) valueOperations.get(giftKey);
            if (count==null) {
                count = 0;
            }
            valueOperations.set(giftKey, count -1);
        }

        if (replaceIds.size()>0) {
            replaceIds.forEach(giftId->{
                //将原礼物插入奖池
                list.leftPush(poolPre + category + ":" + prizeDeployId.intValue(), giftId);

                //更新奖池礼物数量
                String giftKey = giftPre+category+":"+prizeDeployId+":"+giftId;
                Integer count = (Integer) valueOperations.get(giftKey);
                if (count==null) {
                    count = 0;
                }
                valueOperations.set(giftKey, count +1);
            });
        }

        return gifts;
    }

    /**暴击池加水
     * */
    public void addPoolCrit(String poolType, int water) {
        String critKey = "haoya:luck:poolcrit:"+poolType;

        ValueOperations opsForValue = redisTemplate.opsForValue();
        Integer poolCrit = (Integer)opsForValue.get(critKey);
        if (poolCrit == null) {
            poolCrit = 0;
        }
        poolCrit += water;
        opsForValue.set(critKey, poolCrit);
    }

    /**暴击池加水
     * */
    public int getPoolCrit(String poolType) {
        String critKey = "haoya:luck:poolcrit:"+poolType;

        ValueOperations opsForValue = redisTemplate.opsForValue();
        Integer poolCrit = (Integer)opsForValue.get(critKey);
        if (poolCrit == null) {
            poolCrit = 0;
        }
        return poolCrit;
    }
}
